#!/bin/sh

# Build DEB and RPM packages for Linux/x64 and Linux/arm64.

set -e
set -u

myself=${0##*/}
architectures='x64 arm64'
iteration=1

usage()
{
	echo >&2 "Usage: $myself [-i <iteration>]"
	exit 2
}

while getopts i: opt
do
	case $opt in
	i)
		iteration="$OPTARG"
		;;
	\?)
		usage
		;;
	esac
done
shift $((OPTIND - 1))

if ! [ -e 'rebar.config' ] || ! [ -e "tools/$myself" ]
then
	echo >&2 "Please call this script from the repository's root directory."
	exit 2
elif [ $# -ne 0 ]
then
	usage
fi
if ! type fpm >'/dev/null'
then
	echo >&2 "This script requires fpm: https://fpm.readthedocs.io"
	exit 1
fi

rel_name='eturnal'
rel_vsn="$(tools/get-version)"
tmp_dir=$(mktemp -d "/tmp/.$myself.XXXXXX")

trap 'rm -rf "$tmp_dir"' INT TERM EXIT
umask 022

create_scripts()
{
	local dir="$1"

	cat >"$dir/before-install" <<-EOF
	if ! getent group '$rel_name' >'/dev/null'
	then groupadd -r '$rel_name'
	fi
	if ! getent passwd '$rel_name' >'/dev/null'
	then useradd -r -d '/opt/$rel_name' -g '$rel_name' '$rel_name'
	fi
	EOF

	cat >"$dir/after-install" <<-EOF
	chown '$rel_name:$rel_name' '/opt/$rel_name'
	chgrp '$rel_name' '/etc/$rel_name.yml'
	chmod 'o-rw' '/etc/$rel_name.yml'
	EOF

	cat >"$dir/after-remove" <<-EOF
	rm -rf '/opt/$rel_name/run' '/opt/$rel_name/.erlang.cookie'
	if getent passwd '$rel_name' >'/dev/null'
	then userdel '$rel_name'
	fi
	if getent group '$rel_name' >'/dev/null'
	then groupdel '$rel_name'
	fi
	EOF
}

package_architecture()
{
	local target="$1"
	local host_target="$(uname -m)-$target"

	case $host_target in
	x86_64-x64)
		printf 'native'
		;;
	x86_64-arm64)
		printf 'arm64'
		;;
	*)
		echo >&2 "Unsupported host/target combination: $host_target"
		exit 1
		;;
	esac
}

make_package()
{
	local output_type="$1"
	local architecture="$(package_architecture "$2")"
	local dir="$3"

	cd "$dir" # FPM's "--chdir" option doesn't work (as I'd expect).
	fpm --output-type "$output_type" \
	    --input-type 'dir' \
	    --name "$rel_name" \
	    --version "$rel_vsn" \
	    --iteration "$iteration" \
	    --license 'Apache-2.0' \
	    --category 'net' \
	    --provides 'stun-server' \
	    --provides 'turn-server' \
	    --no-depends \
	    --no-auto-depends \
	    --deb-upstream-changelog './NEWS' \
	    --deb-maintainerscripts-force-errorchecks \
	    --deb-systemd-enable \
	    --deb-systemd-auto-start \
	    --deb-systemd "./$rel_name.service" \
	    --deb-init "./$rel_name" \
	    --rpm-init "./$rel_name" \
	    --config-files './etc' \
	    --directories "/opt/$rel_name" \
	    --architecture "$architecture" \
	    --maintainer 'Holger Weiss <holger@zedat.fu-berlin.de>' \
	    --vendor 'ProcessOne, SARL' \
	    --description 'Modern, straightforward STUN and TURN server.' \
	    --url 'https://eturnal.net' \
	    --before-install './before-install' \
	    --after-install './after-install' \
	    --before-upgrade './before-install' \
	    --after-upgrade './after-install' \
	    --after-remove './after-remove' \
	    './opt' './usr' './etc'
	cd "$OLDPWD"
}

for arch in $architectures
do
	tar_name="$rel_name-$rel_vsn-linux-glibc-$arch.tar.gz"
	arch_dir="$tmp_dir/$arch"
	opt_dir="$arch_dir/opt"
	etc_dir="$arch_dir/etc"
	bin_dir="$arch_dir/usr/sbin"
	dst_dir="$opt_dir/$rel_name"

	echo "$myself: Putting together DEB and RPM packages for $arch ..."
	mkdir -p "$opt_dir" "$bin_dir"
	tar -C "$opt_dir" -xzf "$tar_name"

	mv "$dst_dir/bin/eturnalctl" "$bin_dir"
	mv "$dst_dir/etc" "$etc_dir"
	mv "$etc_dir/init.d/$rel_name" "$arch_dir"
	rmdir "$etc_dir/init.d"
	sed -i "s|opt/$rel_name/bin/eturnalctl|usr/sbin/eturnalctl|" \
	    "$etc_dir/systemd/system/$rel_name.service" "$arch_dir/$rel_name"
	create_scripts "$arch_dir"
	make_package 'rpm' "$arch" "$arch_dir"

	mv "$dst_dir/doc/CHANGELOG.md" "$arch_dir/NEWS"
	mv "$etc_dir/systemd/system/$rel_name.service" "$arch_dir"
	rm -r "$etc_dir/systemd"
	make_package 'deb' "$arch" "$arch_dir"

	mv "$arch_dir/$rel_name"?$rel_vsn*.??? .
done
echo "$myself: Created DEB and RPM packages successfully."
